<?php
$inc_path = "./";
require($inc_path."global.php");
require_once(SITE_ROOT. './app/configs/db_session.php');
header("Content-Type: text/html; charset=".$charset);
require(INC_PATH .'xajax/xajaxAIO.inc.php');
uses("trade","industry","member", "setting", "htmlcache");
$industry = new Industries();
$htmlcache = new Htmlcaches();
$member = new Members();
$trade = new Trades();
$setting = new Settings();
$expires = $trade->offer_expires;
if (!isset($_COOKIE[session_name()])) {
	setcookie(session_name(), md5(getRadomStr()), $time_stamp+3*86400);
}
$salt = substr($_COOKIE[session_name()], 0, 10);
setvar("TradeTypes",$trade->getTradeTypes());
setvar("Genders", $member->genders);
setvar("PhoneTypes", $member->phone_types);
setvar("ImTypes", $member->im_types);
$smarty->register_function("format_amount","splitIndustryAmount");
$expires = $trade->offer_expires;
$xajax = new xajax();
$xajax->configure('javascript URI', URL."app/source/xajax/");
$if_visit_post = intval($setting->field("ab", "aa='vis_post'"));
$if_visitpost_auth = intval($setting->field("ab", "aa='vispost_auth'"));
if ($if_visitpost_auth) {
	$smarty->assign("IfVisPostPicture", true);
}
if(!$if_visit_post){
	alert(sprintf(lgg('visitor_forbid'), $_SETTINGS['sitename']));
}
if (isset($_POST['visit_post']) && isset($_POST['offer']['link_man']) && isset($_POST['trade'])) {
	if(empty($_POST['offer']['prim_telnumber'])) exit;
	if ($if_visitpost_auth) {
		$auth_check = uaStrCompare(strtolower($_POST['visit_auth_num']),strtolower($_SESSION['authnum_session']));
		if (!$auth_check) {
			session_destroy();
			alert(lgg('wrong_validate'));
		}else{
		    unset($_SESSION['authnum_session']);
		}
	}
	//check today
	$tVisitLogNum = $g_db->GetOne("select count(id) from ".$tb_prefix."visitlogs where salt='$salt' and  date_line='".date("Ymd")."' and type_name='trades'");
	if ($tVisitLogNum>=3) {
		alert(sprintf(lgg('visit_limit'), 3));
	}
	$vals = array();
	$vals = $_POST['trade'];
	$tmp_result = false;
	$vals['submit_time'] = $vals['created'] = $vals['modified'] = $time_stamp;
	$vals['area_id'] = $_POST['countryid'];
	$vals['province_id'] = $_POST['provinceid'];
	$vals['city_id'] = $_POST['cityid'];
	$vals['type_id'] = strval($_POST['type_id']);
	$trade->setTradeCat($vals['type_id']);
	$vals['content'] = preg_replace("/(\r?\n)\\1+/","\\1",$vals['content']);
	$vals['ip_addr'] = uaGetClientIP();
	$if_check = $setting->field("ab", "aa='vis_post_check'");
	$if_check = intval($if_check);
	$msg = null;
	if ($if_check) {
		$vals['status'] = 0;
		$msg = lgg('wait_check');
	}else{
		$vals['status'] = 1;
		$msg = lgg('success');
	}
	if (isset($_POST['cindustry'])) {
		$industryid = $_POST['cindustry'];
	}else if(isset($_POST['bindustry'])){
		$industryid = $_POST['bindustry'];
	}else if(isset($_POST['aindustry'])){
		$industryid = $_POST['aindustry'];
	}
	if(isset($industryid)) $vals['industry_id'] = $industryid;
	if (array_key_exists($_POST['expire_days'],$expires)) {
		$vals['expire_time'] = $time_stamp+(24*3600*$_POST['expire_days']);
		$vals['expire_days'] = $_POST['expire_days'];
	}else{
		$vals['expire_time'] = $time_stamp+(24*3600*10);
		$vals['expire_days'] = 10;
	}
	$tmp_keywords = preg_replace('#\s+#', ' ', trim($_POST['keywords']));
	$vals['keywords'] = uaConvertComma($tmp_keywords);
	$result = $trade->save($vals);
	if ($result) {
		$last_trade_id = $trade->getMaxId();
		$o_vals = array();
		$o_vals = $_POST['offer'];
		uses("offer", "area");
		$offer = new Offers();
		$area = new Areas();
		if(isset($vals['province_id'])) $o_vals['province_name'] = $area->field("name", "code_id=".$_POST['provinceid']);
		if(isset($vals['city_id'])) $o_vals['city_name'] = $area->field("name", "code_id=".$_POST['cityid']);
		if (isset($industryid)) {
			$o_vals['industry_name'] = $industry->field("name", "id=".$industryid);
		}
		$o_vals['trade_id'] = $last_trade_id;
		$tmp_result = $offer->save($o_vals);
		$g_db->Execute("insert into ".$tb_prefix."visitlogs (salt,date_line,type_name) value ('".$salt."','".date("Ymd")."','trades');");
		uses("stat");
		$stat = new Stats();
		$stat->Add($trade->getTradeCat());
		$industry->updateModelAmount($industryid, $trade->industry_amount_name);
	}
	if ($tmp_result) {
		alert(lgg($msg, false));
	}
}

$xajax->register(XAJAX_FUNCTION,  new xajaxUserFunction('rebuildHTML', 'ajax.php'));
$xajax->processRequest();
setvar('xajax_javascript', $xajax->getJavascript());

setvar("OfferExpires",$expires);
if (isset($_GET['action']) && ($_GET['action'])=="html") {
$smarty->MakeHtmlFile('htmls/post.html',$smarty->fetch($theme_name."/trade_post.html"), true, "post.php");
}
template($theme_name."/trade_post");
?>